package com.iims.customer.service;

import cn.hutool.core.lang.Validator;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.iims.common.constant.UserConstants;
import com.iims.customer.domain.CustomerType;
import com.iims.customer.mapper.CustomerTypeMapper;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 客户类型服务
 *
 * @author fanpei
 * @date 2021/6/11 14:52
 */
@Service
public class CustomerTypeServiceImpl extends ServiceImpl<CustomerTypeMapper, CustomerType> implements ICustomerTypeService {

    @Override
    public List<CustomerType> selectCustomerTypeList(CustomerType custype) {
        return baseMapper.selectList(new LambdaQueryWrapper<CustomerType>()
                .like(StrUtil.isNotBlank(custype.getCusTypeName()), CustomerType::getCusTypeName, custype.getCusTypeName()));
    }

    @Override
    public CustomerType selectOne(Long customerId) {
        return baseMapper.selectById(customerId);
    }

    @Override
    public String checkCustomerTypeNameUnique(CustomerType customer) {
        Long customerId = Validator.isNull(customer.getCusTypeTd()) ? -1L : customer.getCusTypeTd();
        CustomerType info = getOne(new LambdaQueryWrapper<CustomerType>()
                .eq(CustomerType::getCusTypeName, customer.getCusTypeName()).last("limit 1"));
        if (Validator.isNotNull(info) && info.getCusTypeTd().longValue() != customerId.longValue()) {
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
    }

    @Override
    public int insertCustomerType(CustomerType custype) {
        return baseMapper.insert(custype);
    }

    @Override
    public int updateCustomerType(CustomerType custype) {
        return baseMapper.updateById(custype);
    }

    @Override
    public int deleteCustomerTypeById(Long cusTypeId) {
        return baseMapper.deleteById(cusTypeId);
    }

    @Override
    public boolean checkCustomerTypeUse(Long cusTypeId) {
        return baseMapper.checkCustomerTypeUse(cusTypeId) > 0;
    }

}
